package io.renren.modules.generator.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import io.renren.common.utils.ShiroUtils;
import io.renren.modules.sys.entity.SysUserEntity;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.generator.entity.WebContentEntity;
import io.renren.modules.generator.service.WebContentService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 主页内容表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-11-20 11:07:46
 */
@RestController
@RequestMapping("generator/webcontent")
@Api(value = "主页内容",tags = {"主页内容"})
public class WebContentController {
    @Autowired
    private WebContentService webContentService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:webcontent:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = webContentService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{contentId}")
    @RequiresPermissions("generator:webcontent:info")
    public R info(@PathVariable("contentId") Integer contentId){
		WebContentEntity webContent = webContentService.getById(contentId);

        return R.ok().put("webContent", webContent);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:webcontent:save")
    public R save(@RequestBody WebContentEntity webContent){
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        Long userId = userEntity.getUserId();
        webContent.setCreateUser(Math.toIntExact(userId));
        webContent.setCreateTime(new Date());
        webContent.setUpdateTime(new Date());
		webContentService.save(webContent);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:webcontent:update")
    public R update(@RequestBody WebContentEntity webContent){
		webContentService.updateById(webContent);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:webcontent:delete")
    public R delete(@RequestBody Integer[] contentIds){
		webContentService.removeByIds(Arrays.asList(contentIds));

        return R.ok();
    }

}
